package com.atpy.reggie.controller;

import com.atpy.reggie.common.BaseContext;
import com.atpy.reggie.common.R;
import com.atpy.reggie.entity.ShoppingCart;
import com.atpy.reggie.service.ShoppingCartService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车{}",shoppingCart);
        final val currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        final val dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper=new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,currentId);
        if (dishId!=null){
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);


        }else {
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        }
        final val cartServiceOne = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        if (cartServiceOne != null){
            final val number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number+1);
            shoppingCartService.updateById(cartServiceOne);
            return R.success(cartServiceOne);
        }else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }

    }
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){

        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper=new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartLambdaQueryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        final val shoppingCarts = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
        return R.success(shoppingCarts);
    }
    @DeleteMapping("/clean")
    public R<String> delete(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("删除购物车成功");
    }
//    Request URL: http://localhost:8080/shoppingCart/sub
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("shoppingCart{}",shoppingCart);
        final val dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        if (dishId!=null){
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
            final val cart = shoppingCartService.getOne(queryWrapper);
            final val number = cart.getNumber();
            if (number>1){
                cart.setNumber(number-1);
                shoppingCartService.updateById(cart);
            }else {
                shoppingCartService.removeById(cart.getId());
            }
        }else {
            final val setmealId = shoppingCart.getSetmealId();
            queryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
            ShoppingCart cart=shoppingCartService.getOne(queryWrapper);
            final val number = cart.getNumber();
            if (number>1){
                cart.setNumber(number-1);
                shoppingCartService.updateById(cart);
            }else {
                shoppingCartService.removeById(cart.getId());
            }

        }

        return R.success("删除成功");
    }
}
